package com.huanying.goodjob.appraise;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.huanying.framework.user.User;
import com.huanying.framework.user.UserService;
import com.huanying.framework.user.User_Label;
import com.huanying.goodjob.job.JobService;
import com.huanying.goodjob.job.Jobitem;
import com.huanying.goodjob.job.Jobitem_User;

@Controller
public class AppraiseController {
	private static Logger logger = Logger.getLogger(AppraiseController.class);
	@Autowired
	private AppraiseService appraiseService;
	@Autowired
	private UserService userService;
	@Autowired
	private JobService jobService;
	
	//给前台初始化标签列表
	@RequestMapping(value = "/show_label.do",produces = "application/json; charset=utf-8")
	@ResponseBody
	public String show_label(int jobtype_id,String authentic_status,String jsoncallback){
		Map<String, Object> map = new HashMap<String, Object>();
		int authentic_status_new=Integer.parseInt(authentic_status);
		List<Label> label=null;
		try {
			//工人身份（有工种）
			if(authentic_status_new==1) {
				 label=appraiseService.getLabel_worker(authentic_status_new,jobtype_id);
			}
			//工长身份
			else {
				label=appraiseService.getLabel_boss(authentic_status_new);
			}
			map.put("label", label);
			map.put("flag", "1");
			logger.info("标签查找成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			logger.error("操作失败",e);
			map.put("flag", "0");
		}
		Gson gson=new Gson();
	    String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	//添加评价
	@RequestMapping(value = "/add_appraise.do")
	@ResponseBody
	public String add_appraise(int jobitem_user_id,int appraiser_id,int appraiseobject_id,String labels_id,String context,String appraise_score,int flag,String jsoncallback){
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(jobitem_user_id);
		logger.info(appraiser_id);
		logger.info(appraiseobject_id);
		logger.info(labels_id);
		logger.info(context);
		logger.info(appraise_score);
		logger.info(flag);
		List<Label> labels = null;
		String[] label_id_array=null;
		//if(Integer.valueOf(label_id_array[]) ) {}
		if(labels_id!=null||labels_id!="") {
		if(labels_id!="0") { 
		 label_id_array = labels_id.split("_");			
		try {
			labels = appraiseService.getLabelById(label_id_array);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
			logger.info("获取不到标签");
			}
		}
		}

	    try {
	    	Appraise appraise=new Appraise();
	    	Jobitem_User jobitem_user=jobService.getbyId_jobitem_user(jobitem_user_id);
	    	User appraiser=userService.getbyId(appraiser_id);
	    	User appraiseobject=userService.getbyId(appraiseobject_id);
	    	Jobitem jobitem=jobService.getbyId_jobitem(jobitem_user.getJobitem().getId());
	    	
			int sum_appraise1=0;
			int sum_appraise2=0;
			int j=0;
			int k=0;
			 if(flag==1)    //工长评价工人
			 {
				 if(jobitem_user.getStatus()==1)
				 {
					 if(labels != null) {
					 appraise.setAppraiser(appraiser);
					 appraise.setAppraiseobject(appraiseobject);
					 appraise.setLabels(labels);
					 appraise.setDirection(1);
					 appraise.setAppraise_score(Integer.parseInt(appraise_score));
					 appraise.setContext(context);
					 appraise.setJobitem(jobitem);
					 appraise.setCreate_time(new Date());
					 appraise.setStatus(1);
					 appraiseService.add(appraise);
					 
					 //更新用户表的avgscore_worker字段
					List<Appraise> list=appraiseService.getAppraise(appraiseobject.getId());
					for(j=0;j<list.size();j++) {
							sum_appraise1+=list.get(j).getAppraise_score();
					}
					int avgscore_worker=sum_appraise1/j;
					User u=userService.getbyId(appraiseobject.getId());
					u.setAvgscore_worker(avgscore_worker);
					userService.update(u);
					//更新label表
					for(int i=0;i<labels.size();i++) {
						for(int m = 0; m < label_id_array.length; m++) {
							Label label=appraiseService.getbyId_Label(Integer.valueOf(label_id_array[m]));
							label.setAppraiser(appraise);
							appraiseService.updateLabel(label);
						}
					}
					//更新user_label表
					for(int i = 0; i < label_id_array.length; i++) {
						User_Label user_label=new User_Label();
						user_label.setLabel_id(Integer.valueOf(label_id_array[i]));
						user_label.setUser_id(appraiseobject.getId());
						userService.addUser_Label(user_label);
					}					
				 }else {
						 appraise.setAppraiser(appraiser);
						 appraise.setAppraiseobject(appraiseobject);
						 appraise.setDirection(1);
						 appraise.setAppraise_score(Integer.parseInt(appraise_score));
						 appraise.setContext(context);
						 appraise.setJobitem(jobitem);
						 appraise.setCreate_time(new Date());
						 appraise.setStatus(1);
						 appraiseService.add(appraise);
						 
						 //更新用户表的avgscore_worker字段
						List<Appraise> list=appraiseService.getAppraise(appraiseobject.getId());
						for(j=0;j<list.size();j++) {
								sum_appraise1+=list.get(j).getAppraise_score();
						}
						int avgscore_worker=sum_appraise1/j;
						User u=userService.getbyId(appraiseobject.getId());
						u.setAvgscore_worker(avgscore_worker);
						userService.update(u);
				 }
					 
			 }
			 }
			 if(flag==2)  //工人评价工长
			 {
				 if(labels != null) {
				 appraise.setAppraiser(appraiser);
				 appraise.setAppraiseobject(appraiseobject);
				 appraise.setLabels(labels);
				 appraise.setDirection(2);
				 appraise.setAppraise_score(Integer.parseInt(appraise_score));
				 appraise.setContext(context);
				 appraise.setJobitem(jobitem);
				 appraise.setCreate_time(new Date());

				 appraise.setStatus(1);
				 appraiseService.add(appraise);
				 //更新用户表的avgscore_boss字段
				 List<Appraise> list=appraiseService.getAppraise(jobitem.getJob().getUser().getId());
				 for(k=0;k<list.size();k++) {
					sum_appraise2+=list.get(k).getAppraise_score();
				    }
				int avgscore_boss=sum_appraise2/k;
				User u=userService.getbyId(jobitem.getJob().getUser().getId());
				u.setAvgscore_boss(avgscore_boss);
				userService.update(u);
				
				jobitem.setJobitem_status(-2);
				jobService.update_jobitem(jobitem);
				
				//更新label表
				for(int i=0;i<labels.size();i++) {
					for(int m = 0; m < label_id_array.length; m++) {
						Label label=appraiseService.getbyId_Label(Integer.valueOf(label_id_array[m]));
						label.setAppraiser(appraise);
						appraiseService.updateLabel(label);
					}
				}					
				//更新user_label表
				for(int i = 0; i < label_id_array.length; i++) {
					User_Label user_label=new User_Label();
					user_label.setLabel_id(Integer.valueOf(label_id_array[i]));
					user_label.setUser_id(appraiseobject.getId());
					userService.addUser_Label(user_label);
				}	
			 
			 }else {
				 appraise.setAppraiser(appraiser);
				 appraise.setAppraiseobject(appraiseobject);
				 appraise.setDirection(2);
				 appraise.setAppraise_score(Integer.parseInt(appraise_score));
				 appraise.setContext(context);
				 appraise.setJobitem(jobitem);
				 appraise.setCreate_time(new Date());

				 appraise.setStatus(1);
				 appraiseService.add(appraise);
				 //更新用户表的avgscore_boss字段
				 List<Appraise> list=appraiseService.getAppraise(jobitem.getJob().getUser().getId());
				 for(k=0;k<list.size();k++) {
					sum_appraise2+=list.get(k).getAppraise_score();
				    }
				int avgscore_boss=sum_appraise2/k;
				User u=userService.getbyId(jobitem.getJob().getUser().getId());
				u.setAvgscore_boss(avgscore_boss);
				userService.update(u);
				
				jobitem.setJobitem_status(-2);
				jobService.update_jobitem(jobitem);
			 }
				 
			 }	 
			map.put("flag","1"); 
			logger.info("添加成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			map.put("flag", 0);
			logger.error("添加评价失败", e);
			//e.printStackTrace();
		}
	   
	    Gson gson=new Gson();
	    String str=gson.toJson(map);
		return jsoncallback+"("+str+")";
	}
	/*//更新评价
	@RequestMapping("/update_appraise.do")
	@ResponseBody
	public String update_appraise(int id,int label_id,String context,String appraise_score,String jsonrollback){
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info(id);
		logger.info(label_id);
		logger.info(context);
		logger.info(appraise_score);
		   try {
			Appraise appraise=appraiseService.getbyId(id);
			appraise.setCreate_time(new Date());
			if(context!=null && context!="")
			{
				appraise.setContext(context);
				 appraise.setCreate_time(new Date());

			}
			if(appraise_score!=null)
			{
				appraise.setAppraise_score(Integer.parseInt(appraise_score));
				 appraise.setCreate_time(new Date());

			}
			appraiseService.update(appraise);
			map.put("flag", "1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("修改评价失败",e);
			//e.printStackTrace();
		}
		   Gson gson=new Gson();
		   String str=gson.toJson(map);
		   return jsonrollback+"("+str+")";		
	}
	@RequestMapping(value = "/search_appraise.do",method=RequestMethod.GET,produces = "application/json; charset=utf-8")
	@ResponseBody
	public String search_appraise(int user_id,String jsonrollback){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			List list=appraiseService.search_appraise(user_id);
			map.put("list",list);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("查询评价失败", e);
			//e.printStackTrace();
		}
		 Gson gson=new Gson();
		   String str=gson.toJson(map);
		   return jsonrollback+"("+str+")";	
	}*/
}
